package com.yanghui.lingyue.functions.defaultFunction

import com.icedata.sika.bridge.SikaEvent
import com.icedata.sika.bridge.SikaGroupContact
import com.icedata.sika.bridge.SikaGroupEvent
import com.icedata.sika.core.CommandProcessor
import com.icedata.sika.core.Sika
import com.icedata.sika.util.Logger
import com.yanghui.lingyue.bot.Bot
import com.yanghui.lingyue.data.Command
import com.yanghui.lingyue.utils.functionUtils.FunctionUtils
import java.util.*

object AdministratorHandler {

    fun parse(event: SikaEvent, sika: Sika): LinkedList<Command> {
        event as SikaGroupEvent
        val command = LinkedList<Command>()
        val user = Bot.userList[event.sender.id]
        user ?: return command
        if (!user.isAdministrator && event.sendGroup.getUserAsMember(event.sender.id)
                .getPermission() == SikaGroupContact.SikaGroupMember.Permission.Member
        )
            return command
        if (!event.message.contentToString().startsWith("LingYue "))
            return command
        command.add(Command(event, "Admin", ""))
        return command
    }

    fun filter(command: Command): Boolean {
        return command.instruction == "Admin"
    }

    suspend fun execute(command: Command, sika: Sika): CommandProcessor.Result {
        val instruction = FunctionUtils.extractPlainMessage(command.event as SikaGroupEvent).split(" ")
        when (instruction[1]) {
            "-close" -> Bot.groupList[(command.event).sendGroup.id]?.groupCache?.set("OnActive", false)
            "-open" -> Bot.groupList[(command.event).sendGroup.id]?.groupCache?.set("OnActive", true)
            "-reload" -> {
                Logger.logDebug("Admin", "Reload")
                Bot.boot()
            }
            "-broadCast" -> {
                for (id in Bot.groupList.keys) {
                    SikaGroupContact(id).sendMessage(
                        command.event.message.contentToString()
                            .replace("LingYue", "")
                            .replace("-broadCast", "")
                    )
                }
            }
            "-mute" -> {
                val target = FunctionUtils.extractAtMessage(command.event)[0].target
                var time = 30
                var unit = 1
                for (i in instruction) {
                    if (i.contains("-time")) {
                        time = i.replace("-time=", "").toInt()
                    }
                    if (i.contains("-unit=")) {
                        when (i.replace("-unit=", "").toLowerCase()) {
                            "min" -> unit = 60
                            "hour" -> unit = 3600
                            "quarter" -> unit = 15
                            "day" -> unit = 3600 * 24
                        }
                    }
                }
                if (command.sendGroup!!.getUserAsMember(target).getPermission().ordinal
                    >= command.sendGroup!!.getBotAsMember().getPermission().ordinal
                ) {
                    command.sendGroup?.sendMessage("权限不足")
                } else
                    command.sendGroup?.muteUser(target, time * unit)
            }
            "-demute" -> {
                /* 要求必须为实际管理员才可以调用本函数 */
                if (Bot.userList[command.sender.id]!!.isAdministrator) {
                    var group = 0L
                    var userID = 0L
                    for (i in instruction) {
                        if (i.contains("-group=")) {
                            group = i.replace("-group=", "").toLong()
                        }
                        if (i.contains("-userID")) {
                            userID = i.replace("-userID=", "").toLong()
                        }
                    }
                    if (group == 0L) {
                        val target = FunctionUtils.extractAtMessage(command.event)[0].target
                        command.sendGroup?.demuteUser(target)
                    } else {
                        SikaGroupContact(group).demuteUser(userID)
                    }
                }
            }
            "-status" -> {
                command.sendGroup?.sendMessage(Bot.groupList[command.sendGroup?.id]?.groupCache.toString())
            }
        }
        return CommandProcessor.Result.ADOPT
    }
}